---@class Battle.CBattle:Battle.BattleBase
local CBattle = Battle.CreateClass("CBattle",Battle.BattleBase)
Battle.CBattle = CBattle
---@return Battle.CBattle
function CBattle.CreateBattle()
    local bat = CBattle.New()
    bat:Init()
    return bat
end
function CBattle.DestroyBattle()
    if Battle.ClientBattle then
        Battle.ClientBattle:Leave()
    end
end
function CBattle:LoadAndFight()
    math.randomseed(os.time())
    Battle.GlobalEventSystem.BroadCast(Battle.Event.BattleLoadingStart)
    self:SetBattleStage(Battle.BattleStage.Loading)
    Battle.BattleGlobalMgr.RandomWeather()
    --Battle.SceneMgr.LoadScene(Battle.BattleGlobalMgr.GetWeatherScenePath(),function()
    --    Battle.PlayerSystem.CreateLord()
    --    Battle.GlobalEventSystem.BroadCast(Battle.Event.BattleReady)
    --    self:StartFight()
    --end)
    showUI(PanelNames.UILoading, nil, true)
    Battle.SceneMgr.LoadSceneAsync(Battle.BattleGlobalMgr.GetWeatherScenePath(),function()
        Battle.PlayerSystem.CreateLord()
        Battle.GlobalEventSystem.BroadCast(Battle.Event.BattleReady)
        self:StartFight()
    end,nil, true)
end

function CBattle:StartFight()
    self:SetBattleStage(Battle.BattleStage.Init)
    Battle.PlayerSystem.OnInitialize()
    Battle.BattleGlobalMgr.OnInitialize()
    self:StartCountdown(Battle.GlobalConfig.GetInt(10)/1000)
    showUI(PanelNames.UIBattleEntityLayer)
    showUI(PanelNames.UIBattle)
    self:SetBattleStage(Battle.BattleStage.Run)
    Battle.ActorManager.StartLogic()
end


function CBattle:CountdownFinished()
    if self.battleStage ~= Battle.BattleStage.Finished then
        self:BattleFinished()
    else
        self:SetBattleStage(Battle.BattleStage.Result)
    end
end
function CBattle:BattleFinished()
    self:SetBattleStage(Battle.BattleStage.Finished)
    local my = Battle.ActorManager.GetLord(Battle.CampType.Blue)
    local enemy = Battle.ActorManager.GetLord(Battle.CampType.Red)
    local myHp = 0
    local enemyHp = 0
    if my then myHp = my:GetHp() end
    if enemy then enemyHp = enemy:GetHp() end
    self.battleResult = 0
    if myHp > enemyHp then
        self.battleResult = Battle.BattleResultType.Success
    end
    for k,v in pairs(Battle.ActorManager.GetActorList()) do
        v.aiSystem:Stop()
    end
    Battle.GlobalEventSystem.BroadCast(Battle.Event.BattleFinished,self.battleResult)
    self:StartCountdown(1)
end
function CBattle:OnCreate()
    self:AddManager(Battle.GlobalEventSystem)
    self:AddManager(Battle.ActorManager)
    self:AddManager(Battle.SceneMgr)
    self:AddManager(Battle.EffectShowMgr)
    self:AddManager(Battle.BufferMgr)
    self:AddManager(Battle.BattleGlobalMgr)
    self:AddManager(Battle.BulletSystem)
    self:AddManager(Battle.EventManager)
    self:AddManager(Battle.KeyMgr)
    self:AddManager(Battle.VirtualServer)
    self:AddManager(Battle.GrassMgr)
    self:AddManager(Battle.ReportMgr)
    self:AddManager(Battle.GlobalBuffSystem)
    self:AddManager(Battle.PlayerSystem)
    --PoolMgr保证在最后添加，最后释放
    self:AddManager(Battle.PoolMgr)
end
function CBattle:OnDestroy()
    closeUI(PanelNames.UIBattle)
    closeUI(PanelNames.UIBattleEntityLayer)
end
function CBattle:SetCardList(cards,enemyCards)
    Battle.PlayerSystem.SetCardList(cards,enemyCards)
end
function CBattle:CreateCardsByIdList(idList)
    local cardList = {}
    for k,v in pairs(idList) do
        local cfg = DataTable.GetById("t_hero",v)
        if cfg == nil then
            logError("卡组找不到t_hero ："..v)
        else
            table.insert(cardList,Battle.Card.New():Set(v,1))
        end
    end
    return cardList
end